package br.com.javamagazine;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.javamagazine.jdo.UserFotoDAO;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesServiceFactory;

@SuppressWarnings("serial")
public class UpdateServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(UpdateServlet.class
			.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		
		String userId = null;
		Blob fotoBlob = null;
		
		String imageSt = req.getParameter("image");
		
		byte[] textoBytes2 = getImageBytes(imageSt.getBytes());
		
		fotoBlob = new Blob(textoBytes2);

		if ( userId == null ) {
			userId = "123";
		}
		
		UserFotoDAO.saveOrUpdateUserFoto(userId, fotoBlob);
	}
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {

		String userId = null;
		Blob fotoBlob = null;
		
		InputStream ttt = req.getInputStream();
		
		byte[] textoBytes2 = getImageBytesFromInputStream( ttt );
		
		fotoBlob = new Blob(textoBytes2);

		if ( userId == null ) {
			userId = "123";
		}
		
		UserFotoDAO.saveOrUpdateUserFoto(userId, fotoBlob);

	}
	
	private byte[] getImageBytesFromInputStream(InputStream stream) throws IOException{

		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		byte[] buffer = new byte[8192];
		int len = 0;
		while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
			byteOut.write(buffer, 0, len);
		}
		byte[] fotoBytes = byteOut.toByteArray();

		return getImageBytes(fotoBytes);
	}
	
	private byte[] getImageBytes( byte[] fotoBytes ) {
		
		// Redimensiona
		Image foto = ImagesServiceFactory.makeImage(fotoBytes);
		
		//if (foto.getHeight() > 500 || foto.getWidth() > 500) {
//			Transform resize = ImagesServiceFactory.makeResize(
//					foto.getHeight(), foto.getWidth());
//			foto = ImagesServiceFactory.getImagesService()
//					.applyTransform(resize, foto);
		//}
		fotoBytes = foto.getImageData();

		return fotoBytes;
	}
	
	protected byte[] processRequest(HttpServletRequest request, HttpServletResponse response)  
     throws ServletException, IOException {  
         String arquivo = null;  
         response.setContentType("image/jpeg");  
         ServletOutputStream sos = response.getOutputStream();  
         arquivo = request.getParameter("image");  
         String userId = request.getParameter("userId");  
         
         byte[] arq = arquivo != null ? arquivo.getBytes() : null;
         
         //sos.write(arq);
           
         sos.close();
         
         return arq;
     }   
}
